/**
 * 功能描述 表单
 */

define(function (require, exports, module){
	var Form = require("form");
	var aw = require("ajaxwrapper");
	var store =require("store");
	var tools = require("tools");
	var Dialog = require("dialog");
	var activeUser = store.get("user");
	
	var instance = {
		initComponent: function (params,widget){
            var form = new Form({
				parentNode:".J-form",
				show:false,
				saveaction:function(){
        			var data = form.getData();
					aw.saveOrUpdate("api/driverfile/saveDriver",data,function(data){
						widget.get("grid").refresh();
						widget.show([".J-grid"]).hide([".J-form"]);
						widget.get("subnav").show(["add","search","status"]);
					});
					
        			
        		},
        		cancelaction : function(){
           			     widget.show([".J-grid"]).hide([".J-form"]);
    					 widget.get("subnav").show(["add","search","status"]);//.hide(["return"])
    					 widget.get("grid").refresh();
        		},
				model:{
					id:"form",
					layout: "2_1",
					items:[{
						name:"pkDriver",
						type:"hidden",
					},{
						name:"version",
						defaultValue:"0",
						type:"hidden"
					},{
						name:"seal",
						defaultValue:true,
						type:"hidden"
					},{
						name:"name",
						label:"姓名",
						validate:["required"]
					},{
						name:"idNumber",
						label:"身份证号",
						validate: ["required"],
						exValidate: function(value) {
							var reg = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/;
							if(!reg.test(value)){
								return "请输入正确的身份证号！";
							}else{
								return true;
							}
                        },
						events: {
							"blur": function(e){
								if(e.target.value){
									aw.ajax({
										url:"api/driverfile/search",
										data: {
											s: e.target.value,
											searchProperties: "idNumber",
										},
										success: function(data){
											if(data.length != 0){
												Dialog.alert({
								    				content:"此身份证已被录用，请核对身份证！"
								            	});
											}else{
												var birthday = tools.getBirthdateByIdNo(e.target.value);
												var age = tools.getAgeByBirthdate(birthday);
												form.setValue("birthday",birthday);
												form.setValue("age",age);
											}
										}
									});
								}
							}
						}
						
					},{
						name:"birthday",
						label:"出生日期",
						type: "date",
						validate:["required"]
					},{
						name:"age",
						label:"年龄",
						validate:["required"]
					},{
						name:"driveBookType",
						label:"驾驶类型",
						type:"select",
						options:[{
							key:"A1",
							value:"A1"
						},{
							key:"A2",
							value:"A2"
						},{
							key:"A3",
							value:"A3"
						},{
							key:"B1",
							value:"B1"
						},{
							key:"B2",
							value:"B2"
						},{
							key:"C1",
							value:"C1"
						},{
							key:"C2",
							value:"C2"
						},{
							key:"C3",
							value:"C3"
						}],
						validate:["required"]
					},{
						name:"driveBookDate",
						type: "date",
						label:"取得驾证日期",
						validate:["required"],
						events: {
							"blur": function(e){
								if(e.target.value){
									var bookAge = tools.getAgeByBirthdate(e.target.value);
									form.setValue("driveBookAge",bookAge);
								}
							}
						}
					},{
						name:"driveBookAge",
						label:"驾龄",
						validate:["required"]
					},{
						name:"phone",
						label:"联系电话",
						validate:["required"]
					},{
						name:"address",
						label:"联系地址",
						validate:["required"]
					},{
						name:"driverStatus",
						type:"hidden",
						defaultValue:"Free"
					},{
						name:"servicePoint",
						type:"hidden",
						defaultValue:activeUser.servicePoint.pkServicePoint
					}]
				}
			});
			widget.set("form", form);
		}
	};
	module.exports = instance;
});